const SearchData = (setsearchValue: any, key: any, value: any) => {
  if (key === 'keywords') {
    setsearchValue((prevItems: any) => {
      return { ...prevItems, [key]: value }
    })
  }

  if (key === '所在地区') {
    // 添加数组
    setsearchValue((prevItems: any) => {
      prevItems.pca.values = value
      return { ...prevItems }
    })
  }

  if (key === 'qualified') {
    setsearchValue((prevItems: any) => {
      return { ...prevItems, [key]: value }
    })
  }

  if (key === 'trade_plate') {
    setsearchValue((prevItems: any) => {
      return { ...prevItems, [key]: value }
    })
  }

  if (key === 'financing_round') {
    let arr: any = []
    value.map((item: any) => {
      if (item.indexOf('/') !== -1) {
        arr = [...arr, ...item.split('/')]
      } else {
        arr.push(item)
      }
    })
    setsearchValue((prevItems: any) => {
      return { ...prevItems, [key]: arr }
    })
  }

  if (key === 'company_type') {
    setsearchValue((prevItems: any) => {
      return { ...prevItems, [key]: value }
    })
  }

  if (key === 'time_scope') {
    setsearchValue((prevItems: any) => {
      prevItems[key].start_time = value[0] || null
      prevItems[key].end_time = value[1] || null
      return { ...prevItems }
    })
  }
  // if (key === 'start_time') {
  //   setsearchValue((prevItems: any) => {
  //     return { ...prevItems, time_scope: { ...prevItems.time_scope, [key]: value } }
  //   })
  // }

  // if (key === 'end_time') {
  //   setsearchValue((prevItems: any) => {
  //     return { ...prevItems, time_scope: { ...prevItems.time_scope, [key]: value } }
  //   })
  // }
}

const isShowSearchTags = (searchValue: any) => {
  return (
    searchValue['keywords'] ||
    searchValue['pca']?.values.length > 0 ||
    searchValue['qualified']?.length > 0 ||
    searchValue['trade_plate']?.length > 0 ||
    searchValue['financing_round']?.length > 0 ||
    searchValue['company_type']?.length > 0 ||
    searchValue['time_scope']?.start_time ||
    searchValue['time_scope']?.end_time ||
    searchValue['hs_ind_list']?.values.length > 0
  )
}

const searchTagsStr = (searchValue: any, key: string) => {
  const keysList = [
    { label: '关键词', value: 'keywords' },
    { label: '所在地区', value: 'pca' },
    { label: '荣誉资质', value: 'qualified' },
    { label: '上市情况', value: 'trade_plate' },
    { label: '融资阶段', value: 'financing_round' },
    { label: '公司类型', value: 'company_type' },
    { label: '成立时间', value: 'time_scope' },
    { label: '产业类型', value: 'hs_ind_list' }
  ]
  for (let index = 0; index < keysList.length; index++) {
    const element = keysList[index]
    if (element.value === key) {
      let valueStr = ''
      if (['keywords'].includes(key)) {
        valueStr = searchValue[key]
      }
      if (['qualified', 'trade_plate', 'financing_round', 'company_type'].includes(key)) {
        valueStr = searchValue[key]?.map((ele: any) => ele)?.join('、')
      }
      if (['pca', 'hs_ind_list'].includes(key)) {
        valueStr = searchValue[key]?.values?.map((ele: any) => ele.show)?.join('、')
      }
      if (['time_scope'].includes(key)) {
        if (searchValue[key].start_time || searchValue[key].end_time) {
          valueStr = `${searchValue[key].start_time ?? ''} -- ${searchValue[key].end_time ?? ''}`
        }
      }
      return {
        label: element.label ?? '',
        value: valueStr,
        isShow: !!valueStr
      }
    }
  }
  return {
    label: '',
    value: '',
    isShow: false
  }
}

export { SearchData, searchTagsStr, isShowSearchTags }
